package com.study.ui.home.adapter

import android.view.View
import androidx.databinding.DataBindingUtil
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.lbj.core.adapter.CoreAdapter
import com.lbj.core.tool.data.SimpleTitle
import com.study.R
import com.study.databinding.AdapterMainBinding

class MainAdapter : CoreAdapter<SimpleTitle>(R.layout.adapter_main) {

    override fun convert(holder: BaseViewHolder, item: SimpleTitle) {
        DataBindingUtil.bind<AdapterMainBinding>(holder.itemView)?.apply {
            index = holder.adapterPosition
            result = item
            presenter = Presenter()
            executePendingBindings()
        }
    }

    inner class Presenter {
        fun onItemClick(v: View, index: Int) {
            val news = data
            news.clear()
            news.addAll(data)
            setDiffData(news)
        }
    }

    override fun isItemSame(old: SimpleTitle, new: SimpleTitle) = old.id == new.id

    override fun isContentsSame(old: SimpleTitle, new: SimpleTitle) =
        old.title == new.title && old.select == new.select
}

